import Vue from 'vue'
import { login, getInfo, logout, refreshToken } from '@/api/login'
import { ACCESS_TOKEN, ACCESS_TOKEN_INFO } from '@/store/mutation-types'
import { welcome } from '@/utils/util'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    permissions: [],
    info: {},
    levelMap: {
      1: {
        id: 'group_owner',
        text: '群主',
        employment: {
          self: 10000 * 100, // 后端单位 分
          team: 20000 * 100
        }
      },
      2: {
        id: 'elite',
        text: '精英',
        employment: {
          self: 80000 * 100,
          team: 200000 * 100
        }
      },
      3: {
        id: 'strongman',
        text: '大咖', // 大咖直接满进度
        employment: {
          self: 1,
          team: 1
        }
      }
    },
    tokenInfo: {},
    revenuesType: {
      1: { text: '下单', eg: '下单' },
      2: { text: '下单-不可提现', eg: '下单-不可提现' },
      3: { text: '撤单', eg: '撤单' },
      4: { text: '撤单-不可提现', eg: '撤单-不可提现' },
      5: { text: '佣金', eg: '佣金' },
      6: { text: '撤销佣金', eg: '撤销佣金' },
      7: { text: '直接邀请 奖励', eg: '直接邀请 奖励' },
      8: { text: '间接邀请 奖励', eg: '间接邀请 奖励' },
      9: { text: '撤销直接邀请奖励', eg: '撤销直接邀请奖励' },
      10: { text: '撤销间接邀请奖励', eg: '撤销间接邀请奖励' },
      11: { text: '培养奖励', eg: '培养奖励' },
      12: { text: '撤销培养奖励', eg: '撤销培养奖励' },
      13: { text: '换货运费补偿', eg: '换货运费补偿' },
      14: { text: '补偿', eg: '补偿' },
      15: { text: '退货退款', eg: '退货退款' },
      16: { text: '退货退款-不可提现', eg: '退货退款-不可提现' },
      17: { text: '退货退款撤销佣金', eg: '退货退款撤销佣金' },
      18: { text: '退货撤销奖励 (直接邀请)', eg: '退货撤销奖励 (直接邀请)' },
      19: { text: '退货撤销奖励 (间接邀请)', eg: '退货撤销奖励 (间接邀请)' },
      20: { text: '充值', eg: '充值' },
      21: { text: '充值-不可提现', eg: '充值-不可提现' },
      22: { text: '充返金额 (不可提现)', eg: '充返金额 (不可提现)' },
      23: { text: '提现', eg: '提现' },
      24: { text: '提现拒绝', eg: '提现拒绝' },
      25: { text: '每日下单奖励', eg: '每日下单奖励' },
      26: { text: '每月下单奖励', eg: '每月下单奖励' },
      27: { text: '划拨', eg: '划拨' },
      28: { text: '平台减少不可提现余额', eg: '平台减少不可提现余额' },
      29: { text: '平台增加不可提现余额', eg: '平台增加不可提现余额' },
      30: { text: '平台减少余额', eg: '平台减少余额' },
      31: { text: '平台增加余额', eg: '平台增加余额' },
      32: { text: '撤销每日下单活动奖励', eg: '撤销每日下单活动奖励' },
      33: { text: '撤销每月下单活动奖励', eg: '撤销每月下单活动奖励' },
      34: { text: '撤单-退运费', eg: '撤单-退运费' },
      35: { text: '撤单-退运费(不可提现)', eg: '撤单-退运费(不可提现)' },
      36: { text: '接龙转入', eg: '接龙转入', value: 36 }
    }
  },

  mutations: {
    SET_TOKEN_INFO_FROM_RESPONSE: (state, tokenInfo) => {
      Vue.ls.set(ACCESS_TOKEN, tokenInfo.token, 7 * 24 * 60 * 60 * 1000)
      state.token = tokenInfo.token
      Vue.ls.set(ACCESS_TOKEN_INFO, tokenInfo, 7 * 24 * 60 * 60 * 1000)
      state.tokenInfo = tokenInfo
    },
    SET_TOKEN_INFO: (state, tokenInfo) => {
      state.tokenInfo = tokenInfo
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          commit('SET_TOKEN_INFO_FROM_RESPONSE', response)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    RefreshToken ({ commit }) {
      return new Promise((resolve, reject) => {
        refreshToken().then(response => {
          commit('SET_TOKEN_INFO_FROM_RESPONSE', response)
          resolve()
        }).catch(err => reject(err))
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const result = response

          if (result.permissions && result.permissions.length >= 0) {
            commit('SET_PERMISSIONS', result.permissions)
            commit('SET_INFO', result)
          } else {
            reject(new Error('getInfo: roles must be a non-null array !'))
          }

          commit('SET_NAME', { name: result.real_name, welcome: welcome() })
          // commit('SET_AVATAR', result.avatar)

          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        }).finally(() => {
          commit('SET_TOKEN', '')
          commit('SET_TOKEN_INFO', {})
          commit('SET_PERMISSIONS', [])
          Vue.ls.remove(ACCESS_TOKEN)
          Vue.ls.remove(ACCESS_TOKEN_INFO)
        })
      })
    }
  }
}

export default user
